<template>
	<view>
		<view class="container" v-if="flag">
			<view class="current"><text>当前可用金币: {{userJinbi}}</text> <text class="current_pay" @click="toPay">去充值</text></view>
			<view>
				<view v-if="this.orderList.length > 0">
					<view class="list-block" v-for="(item,index) in orderList" :key="item.id">
						<view class="list">
							<text class="time">{{item.createtime}}</text>
							<text class="title">{{item.type_text}}金币{{item.jinbi}}个</text>
							<text class="state" :class="'state'+item.status">{{item.status_text}}</text>
						</view>
					</view>
				</view>
				<view v-else class="nothing">暂无订单</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				orderList: [],
				flag: 0,
				userJinbi: 0
			}
		},
		computed: {
			...mapState(['userinfo'])
		},
		watch: {
			// 检测userinfo的变化
			userinfo(newVal) {
				//userinfo 变好了
				console.log('watch userinfo变化了')

				if (newVal) {
					// 如果 newVal 不为空,说明刚刚是登录了
					this.user = this.userinfo
				} else {
					// 如果 newVal 为空,表示刚刚是退出了
					this.user = {}

					this.$store.dispatch('clear_userinfo')
				}
			}
		},
		onShow() {
			//未登录检测,跳转到登录页面
			if (!this.userinfo) {
				uni.showModal({
					content: '请先微信登录',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/login?reffer=' + window.location.href
							})
						} else if (res.cancel) {
							uni.switchTab({
								url: '/pages/tabbar/index/index'
							})
						}
					}
				});

				return
			}
			
			// 微信分享
			this.shareWx('', '充值订单')	
			
			// 获取订单
			this.getMyOrder()

		},
		methods: {//打开充值页面
			toPay(){
				//未登录检测,跳转到登录页面
				if (!this.userinfo) {
					this.login()
					return
				}
				uni.navigateTo({
					url: '/pages/pay/pay'
				})
			},
			getMyOrder() {
				this.$api.getPayOrder({
					'id': this.userinfo.id
				}).then(res => {
					this.flag = 1
					if(res.code == 1){
						this.orderList = res.data.orderList
						this.userJinbi = res.data.userJinbi
					}
					console.log(this.orderList)
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 15px;
		
		.current{
			margin-bottom: 10px;
		}
		.current_pay{
			margin-left: 16px;
			padding: 4px;
			color: #fff;
			background: #f07373;
			border-radius: 4px;
		}

		.list-block{
			padding: 8px 0;
			border-bottom: 1px solid #ccc;
		}
		
		.list {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 8px 0;

			.time {
				font-size: 13px;
			}

			.title {
				padding: 0 5px;
				flex: 1;
			}

			.state0 {
				color: #2c3e50;
			}

			.state1 {
				color: #3c8dbc;
			}

			.state2 {
				color: #e74c3c;
			}

			.state3 {
				color: #f39c12;
			}
		}
		
		.nothing{
			color: rgb(119, 119, 119);
			text-align: center;
		}

	}
</style>

